<html>
<head>
<title>WordPress to Typecho 博客数据转移工具</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
</head>
<body>
<?php
/*
WordPress to Typecho 博客数据转移工具
*/

$ip = "127.0.0.1";
$pot = 3306;
$usr = "root";
$psw = "xzx";
$from_db = "wordpress";//源数据库
$to_db = "typecho";//目标数据库
$oldToNew = array(
 'template' => 'theme',
 'blog_charset' => 'charset',
 'html_type' => 'contentType',
 'gzipcompression' => 'gzip',
 'blogname' => 'title',
 'blogdescription' => 'description',
 'require_name_email' => 'commentsRequireMail|commentsRequireURL',
 'comment_moderation' => 'commentsRequireModeration',
 'active_plugins' => 'plugins',
 'links_updated_date_format' => 'commentDateFormat',
 'siteurl' => 'siteUrl',
 'default_category' => 'defaultCategory',
 'users_can_register' => 'allowRegister',
 'default_comment_status' => 'defaultAllowComment',
 'default_ping_status' => 'defaultAllowPing',
 'posts_per_page' => 'pageSize',
 'posts_per_rss' => 'commentsListSize',
 'date_format' => 'postDateFormat',
 'thread_comments_depth' => 'commentsMaxNestingLevels',
 'thread_comments' => 'commentsThreaded',
 'comments_per_page' => 'commentsPageSize',
 'default_comments_page' => 'commentsPageDisplay',
 'comment_order' => 'commentsOrder',
 'show_avatars' => 'commentsAvatar',
 'avatar_rating' => 'commentsAvatarRating');
$i = 0;
$mysqli = new mysqli($ip,$usr,$psw,$from_db,$pot);
$str = "show databases;";
$j = 0;

function getUnixTimestamp($date) {
	date_default_timezone_set("Asia/Shanghai"); 
	
	$year=((int)substr($date,0,4));//取得年份 

	$month=((int)substr($date,5,2));//取得月份 

	$day=((int)substr($date,8,2));//取得几号 
	
	$hour=((int)substr($date,11,2));//取得几号
	
	$min=((int)substr($date,14,2));//取得几号
	
	$sec=((int)substr($date,17,2));//取得几号

	return mktime($hour,$min,$sec,$month,$day,$year);
}

echo "MySQL version is ".$mysqli->server_info."<br/><hr>";
//修正MySQL编码
if($mysqli->character_set_name() != "utf8")	$mysqli->set_charset("utf8"); echo "默认编码不是UTF-8,已改。<br/>";

//查询 wp_users
//$str = "SELECT * FROM `wp_users`";

//查询 wp_posts
//$str = "SELECT * FROM `wp_posts` where `post_status` = 'publish'";

//查询 wp_comments
//$str = "SELECT * FROM `wp_comments`";

//查询 wp_options
//$str = "SELECT * FROM `wp_options` LIMIT 0 , 88";

//查询 wp_terms 和 wp_term_taxonomy
//$str = "SELECT a.term_id,a.name,a.slug,b.taxonomy,b.description,b.count FROM `wp_terms` a,`wp_term_taxonomy` b where a.term_id = b.term_id";

//查询 wp_term_relationships
//$str = "SELECT * FROM `wp_term_relationships`";

if ($mysqli->multi_query($str)) {
	do {
		$result = $mysqli->store_result();
		while ($row = $result->fetch_row()) {
			$j++;
			//转移 wp_users 至 typecho_users
			//$str = "INSERT INTO `typecho_users`(`uid`, `name`, `password`, `mail`, `url`, `screenName`, `group`) VALUES (".$row[0].",'".$row[1]."','".$row[2]."','".$row[4]."','".$row[5]."','".$row[9]."','subscriber')";
			
			//转移 wp_posts 至 typecho_contents
			//$str = "INSERT INTO `typecho_contents`(`cid`, `title`, `slug`, `created`, `text`, `authorId`, `type`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`) VALUES (".$row[0].",'".$row[5]."','".$row[12]."',".getUnixTimestamp($row[2]).",'".$mysqli->real_escape_string($row[4])."',".$row[1].",'".$row[21]."',".$row[23].",1,1,1)";
			
			//转移 wp_comments 至 typecho_comments
			//$str = "INSERT INTO `typecho_comments`(`coid`, `cid`, `created`, `author`, `authorId`, `ownerId`, `mail`, `url`, `ip`, `agent`, `text`, `parent`) VALUES (".$row[0].",".$row[1].",".getUnixTimestamp($row[6]).",'".$mysqli->real_escape_string($row[2])."',".$row[14].",3,'".$row[3]."','".$row[4]."','".$row[5]."','".$row[11]."','".$mysqli->real_escape_string($row[8])."',".$row[13].")";
			
			//转移 wp_options 至 typecho_options
			//defaultAllowComment、defaultAllowPing、commentsPageDisplay 几个字段手动处理.
			/*reset($oldToNew);
			while ($now = key($oldToNew)) {
				if ($now == $row[2]) {
					$str = "UPDATE `typecho_options` SET `value`='".$row[3]."' WHERE `name`='".current($oldToNew)."'";
				}
				next($oldToNew);
			}*/
			
			//转移 wp_terms 和 wp_term_taxonomy 至 typecho_metas
			//$str = "INSERT INTO `typecho_metas`(`mid`, `name`, `slug`, `type`, `description`, `count`) VALUES (".$row[0].",'".$mysqli->real_escape_string($row[1])."','".$row[2]."','".strval($row[3] == 'post_tag' ? 'tag' : $row[3])."','".$row[4]."',".$row[5].")";
			
			//转移 wp_term_relationships 至 typecho_relationships
			//$str = "INSERT INTO `typecho_relationships`(`cid`, `mid`) VALUES (".$row[0].",".$row[1].")";
			
			$mysqli->select_db($to_db);
			if ($mysqli->query($str)) { $i++;}else{ echo "错误SQL语句为：".$str."<br/><br/>";}
		}
		$result->free();
	} while ($mysqli->more_results());
}
echo "<hr>";
echo "<h1>成功插入".$i."条数据,共".$j."条,失败".($j-$i)."条<h1>";
$mysqli->close();
?>
</body>
</html>
